% PURPOSE: Returns results for Table 8 of paper with characteristic based portfolios as the investment universe..
% INPUTS:
%  char_no : Number representing the set of characteristic based portfolios
%  to optimize over
% mean_shrink_setting : Parameter deciding whether to shrink means using
% James-Stein estimator.
% complete_port_setting : Parameter deciding whether to include riskless
% rate as an asset in the portfolio strategy.
%SampleStart = The start date for sample and for Galton learning
%SampleEnd   = The end date for the sample. Galton learning stops "post_window" E months earlier
%
%PARAMETERS:
%pre_window=The historical sample estimation window H
%post_window=The period of the subsequent realized window E
%learning_window=The window for Galton to pre-learn and others to skip
%number_stocks = the number of largest stocks to include in investment
%universe
%stats = The statistics from the horserace requested for output and
%display. These are calculated in TradingStrategy.m
%
%OUTPUT:
%  table_out: horse race statistics
%  names: names of strategies included in the horse race

function [table_out, names] = Table_Char_Ports(char_no,SampleStart,SampleEnd)

    filenames = {'ME_BTM.csv'; 'OP_INV.csv';'ME_BETA.csv';'ME_Prior.csv'};
    filename = (filenames{char_no});
    panel = split(filename,".");
    panel = strcat("Table 7, Panel ", num2str(char_no) , ". " ,panel(1));
    display(panel)
    dataOut = csvread(filename,1);

    data = GetSample();
    data.dataOut = dataOut;
    data.dates = dataOut(:,1);

    data.SampleStart = SampleStart;
    data.SampleEnd   = SampleEnd;

    learning_window=120; %The window for Galton to pre-learn and others to skip
    pre_window=120; %The estimation window
    post_window=12; %The period of the subsequent realized window
    
     stats = {'sharpe'; 'activeShare';'turnover';...
    'min';'max';'mean_std_weights';'std_std_weights';...
    'neg_weights';
     };

    [ eret, T ] = data.getExcessReturnsFromFile();

    galtonCorr = GaltonCorrection;
    galtonCorr.pre_window=pre_window; %The estimation window
    galtonCorr.post_window=post_window; %The period of the subsequent realized window
    galtonCorr = galtonCorr.getGaltonCoefficients(eret,T);

    galtonCorr.output_galton();%Outputs the Galton coefficients associated with the chosen portfolios and parameters

    galtonMV = GaltonStrategyMV;
    galtonMV.galtonCorrection = galtonCorr;

    galtonEB =GaltonEBStrategyMV;
    galtonEB.galtonCorrection = galtonCorr;
    
    galtonGMV = GaltonStrategyGMV;
    galtonGMV.galtonCorrection = galtonCorr;
    
  
    tradingStrategyArray = [galtonMV galtonEB galtonGMV
        ]; 
    stock_ids = [1:size(eret,1)]'*ones([1,T]);
    hr = HorseRace;
    hr.learning_window=learning_window; 
    hr.pre_window=pre_window;
    hr.post_window=post_window;
    hr.rf = data.getRf();
    hr = hr.chooseHorses(tradingStrategyArray);
    hr = hr.runHorserace(eret,stock_ids);
    hr = hr.computeHorseraceResults();
    hr.outputPerformanceResults(stats);
    hr.outputRiskResults;
    
    table_out = hr.performanceRiskResults;
    names = hr.tradingStrategyNames;   

end

